﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ReportService.Models;
using ReportService.Models.ViewModels;
using ReportService.Common;

namespace ReportService.Controllers
{
    public class ReportController : Controller
    {
        private ReportModel _model = new ReportModel();
        private ReportServices _service = new ReportServices();

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (Session["DATAMODEL"] == null)
            {
                this._model.ListTOU = _service.GetListTOU();
                this._model.PriceBoard = _service.GetPriceBoard();
                this._model.ListAllStores = _service.GetListStore();
                this._model.ListAllDevices = _service.GetListDevice();
                this._model.ListReportYears = _service.GetReportYears();

                Session.Add("DATAMODEL", this._model);
            }
            else
            {
                this._model = Session["DATAMODEL"] as ReportModel;
            }
        }

        public ActionResult Index()
        {
            if (!string.IsNullOrEmpty(this._model.StoreReport.StoreName))
            {
                ViewData["DeviceOfSelectedStore"] = LoadDevice(this._model.StoreReport.StoreName);
            }
            return View(this._model);
        }

        public void SavePriceBoard(ReportModel m, string[] Price, string[] PriceID)
        {
            _service.SavePriceBoard(Price, PriceID);
            this._model.PriceBoard = _service.GetPriceBoard();
            Session.Add("DATAMODEL", this._model);
        }

        public void SelectTab(string tab)
        {
            this._model.CurrentSelectTab = tab;
            Session.Add("DATAMODEL", this._model);
        }

        #region Store Report
        public ActionResult ShowStoreReport(ReportModel m)
        {
            IEnumerable<GetAED_DataOfStoreByPeriodResult> reportData = new List<GetAED_DataOfStoreByPeriodResult>();
            string HTML = "<h3>Report cannot be generated. An error has occurred.</h3>";

            try
            {
                if (ShouldReload(m.StoreReport))
                {
                    reportData = _service.GetStoreReport(m.StoreReport).ToList();
                    Session.Add("STORE_REPORT_DATA", reportData);
                }
                else
                {
                    //Cache for export excel
                    reportData = Session["STORE_REPORT_DATA"] as IEnumerable<GetAED_DataOfStoreByPeriodResult>;
                }
                HTML = _service.GenerateStoreReportHTML(reportData, this._model.ListTOU, m.StoreReport);
            }
            catch { }

            ViewData["ReportData"] = HTML;

            return View("StoreReportContent");
        }

        public void StoreReport2Excel(ReportModel m)
        {
            IEnumerable<GetAED_DataOfStoreByPeriodResult> reportData = new List<GetAED_DataOfStoreByPeriodResult>();
            string HTML = "<h3>Report cannot be generated. An error has occurred.</h3>";

            try
            {
                if (ShouldReload(m.StoreReport))
                {
                    reportData = _service.GetStoreReport(m.StoreReport).ToList();
                    Session.Add("STORE_REPORT_DATA", reportData);
                }
                else
                {
                    //Cache for export excel
                    reportData = Session["STORE_REPORT_DATA"] as IEnumerable<GetAED_DataOfStoreByPeriodResult>;
                }
                HTML = _service.GenerateStoreReportHTML(reportData, this._model.ListTOU, m.StoreReport);
            }
            catch { }
            string xmlstring = "<ReportData>" + HTML + "</ReportData>";
            ReportUtils.SendExportFile(xmlstring, Request.MapPath("~/Content/StoreReportContent.xsl"), "StoreReport.xls");
        }

        public string LoadDevice(string selectedStore)
        {
            string retVal = string.Empty;
            var query = this._model.ListAllDevices.Where(p => p.Name.Contains(selectedStore + "."));
            foreach (GetDevicesResult device in query)
            {
                retVal += "<input class=\"store-device\" type=\"checkbox\" value=\"" + device.Name + "\" checked=\"checked\" /><span>" + device.Name.Replace(this._model.StoreReport.StoreName + ".", "") + "</span><br />";
            }

            return retVal;
        }

        private bool ShouldReload(VMStoreReport m)
        {
            if (m.StoreName == this._model.StoreReport.StoreName
                && m.FromDate == this._model.StoreReport.FromDate
                && m.ToDate == this._model.StoreReport.ToDate
                && Session["STORE_REPORT_DATA"] != null)
            {
                return false;
            }

            this._model.StoreReport.StoreName = m.StoreName;
            this._model.StoreReport.FromDate = m.FromDate;
            this._model.StoreReport.ToDate = m.ToDate;

            Session.Add("STORE_REPORT_DATA", this._model);
            return true;
        }
        #endregion

        #region Summary Report
        public ActionResult ShowSummaryReport(ReportModel m)
        {
            IEnumerable<GetAED_SummaryDataOfStoreByYearResult> reportData = new List<GetAED_SummaryDataOfStoreByYearResult>();
            string HTML = "<h3>Report cannot be generated. An error has occurred.</h3>";

            try
            {
                if (ShouldReload(m.SummaryReport))
                {
                    reportData = _service.GetStoreSummaryReport(m.SummaryReport.YearOfReport).ToList();
                    Session.Add("SUMMARY_REPORT_DATA", reportData);
                }
                else
                {
                    //Cache for export excel
                    reportData = Session["SUMMARY_REPORT_DATA"] as IEnumerable<GetAED_SummaryDataOfStoreByYearResult>;
                }

                // generate html 
                HTML = _service.GenerateSummaryReportHTML(reportData, m.SummaryReport.ListExceptStore);
            }
            catch { }

            ViewData["SummaryReportData"] = HTML;

            return View("StoreSummaryReportContent");
        }

        public void SummaryReport2Excel(ReportModel m)
        {
            IEnumerable<GetAED_SummaryDataOfStoreByYearResult> reportData = new List<GetAED_SummaryDataOfStoreByYearResult>();
            string HTML = "<h3>Report cannot be generated. An error has occurred.</h3>";

            try
            {
                if (ShouldReload(m.SummaryReport))
                {
                    reportData = _service.GetStoreSummaryReport(m.SummaryReport.YearOfReport).ToList();
                    Session.Add("SUMMARY_REPORT_DATA", reportData);
                }
                else
                {
                    //Cache for export excel
                    reportData = Session["SUMMARY_REPORT_DATA"] as IEnumerable<GetAED_SummaryDataOfStoreByYearResult>;
                }

                // generate html 
                HTML = _service.GenerateSummaryReportHTML(reportData, m.SummaryReport.ListExceptStore);
            }
            catch { }

            string xmlstring = "<SummaryReportData>" + HTML + "</SummaryReportData>";
            ReportUtils.SendExportFile(xmlstring, Request.MapPath("~/Content/StoreSummaryReportContent.xsl"), "StoreSummaryReport.xls");
        }

        private bool ShouldReload(VMSummaryReport m)
        {
            if (m.YearOfReport == this._model.SummaryReport.YearOfReport
                && Session["SUMMARY_REPORT_DATA"] != null)
            {
                return false;
            }
            this._model.SummaryReport.YearOfReport = m.YearOfReport;

            Session.Add("SUMMARY_REPORT_DATA", this._model);
            return true;
        }
        #endregion

    }
}
